﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Elkomp;

namespace MedicinskiUslugi {

	public partial class frmKorisniciEdit : FormaEdit {

		public frmKorisniciEdit()
			: base() {
			InitializeComponent();
		}

		int ID;
		SqlCommand com = new SqlCommand();

		public frmKorisniciEdit( int selectedId )
			: base() {
			InitializeComponent();
			FormIsEdit = true;
			ID = selectedId;
		}

		private void sredi_Dolzina() {
			foreach ( Control control in groupVnes.Controls ) {
				if ( control is TxtNumeric.TxtNum ) {
					for ( int i = 0; i < medicinskiUslugiDataSet.tblUsers.Columns.Count; i++ ) {
						if ( ( ( TxtNumeric.TxtNum )control ).Name.ToUpper().StartsWith( medicinskiUslugiDataSet.tblUsers.Columns[i].ColumnName.ToUpper() ) ) {
							string ColName = this.medicinskiUslugiDataSet.tblUsers.Columns[i].ColumnName;

							string sql = @"select
                                            isNull(CHARACTER_MAXIMUM_LENGTH,0) + isNull(NUMERIC_PRECISION,0) as Dolzina
	                                        from INFORMATION_SCHEMA.COLUMNS
	                                        where TABLE_NAME='" + medicinskiUslugiDataSet.tblUsers.TableName + "' and COLUMN_NAME ='" + ColName + "'";

							try {
								com.Connection = this.tblUsersTableAdapter.Connection;
								com.Connection.Open();

								com.CommandText = sql;
								com.CommandType = CommandType.Text;

								( ( TxtNumeric.TxtNum )control ).MaxLength = int.Parse( com.ExecuteScalar().ToString() );

								if ( ( ( TxtNumeric.TxtNum )control ).MaxLength < 25 ) {
									( ( TxtNumeric.TxtNum )control ).Size = new System.Drawing.Size( ( ( TxtNumeric.TxtNum )control ).MaxLength * 9, ( ( TxtNumeric.TxtNum )control ).Size.Height );
								} else {
									( ( TxtNumeric.TxtNum )control ).Anchor = ( ( System.Windows.Forms.AnchorStyles )( ( ( System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left ) ) ) );
								}

								( ( TxtNumeric.TxtNum )control ).Location = new Point( 170, ( ( TxtNumeric.TxtNum )control ).Location.Y );

								com.Connection.Close();
							}
							catch ( Exception ex ) {
								MessageBox.Show( ex.ToString() );
							}
						}
					}
				}
			}
		}

		private void frmKorisniciEdit_Load( object sender, EventArgs e ) {
			if ( FormIsEdit ) {
				this.tblUsersTableAdapter.Fill( this.medicinskiUslugiDataSet.tblUsers );
				this.tblUsersBindingSource.Filter = " " + PrimaryKey + " =" + ID;
			} else {
				this.bindingNavigatorAddNewItem.PerformClick();
			}

			this.tblUsersBindingNavigator.Visible = false;
			Oznaci_Zadolzitelni();
			sredi_Dolzina();
		}

		private void btnOk_Click( object sender, EventArgs e ) {
			if ( validacija() ) {
				this.Validate();
				this.tblUsersBindingSource.EndEdit();
				this.tableAdapterManager.UpdateAll( this.medicinskiUslugiDataSet );

				if ( this.FormIsEdit ) {
					save_Edit();
				} else {
					save_Insert();
				}
			}
		}

		private void tblUsersBindingNavigatorSaveItem_Click( object sender, EventArgs e ) {
			this.Validate();
			this.tblUsersBindingSource.EndEdit();
			this.tableAdapterManager.UpdateAll( this.medicinskiUslugiDataSet );
		}
	}
}